// pages/database/database.js
const app = getApp();
const { httpServer } = app.globalData;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    searchVla: "",

    page: 1,
    isLoading: false,
    hasMore: true,
    disabled: false,

    startX: 0, // 开始坐标
    delIndex: -1, // 当前滑动的元素下标位置
  },

  // 搜索框值改变事件
  searchChange(e) {
    const { value } = e.detail;
    this.setData({
      searchVla: value
    })
  },
  
  // 搜索按钮
  search() {
    console.log("搜索按钮", this.data.searchVla)
    this.setData({
      page: 1,
      list: []
    })
    this.getListFn();
  },

  // 获取列表数据
  async getListFn() {
    try {
      let { page, list, searchVla } = this.data;
      let name = '';
      let id_number = '';
      console.log(searchVla)
      if (searchVla.trim() === '' || isNaN(Number(searchVla.trim()))) {
        name = searchVla;
      } else {
        id_number = searchVla;
      }
      let result = await httpServer("post", "/api/Photo/listInformation", {
        pagesize: 10,
        page,
        name,
        id_number,
      })

      const { data } = result;
      list = list.concat(data);
      this.setData({
        list
      })
      if (data.length < 10) {
        this.setData({ 
          isLoading: true,
          hasMore: false,
        });
      }

    } catch (error) {
      this.setData({ 
        isLoading: true,
        page: page - 1,
      });
    }
  },

  // 查看详情
  seeDetails(e) {
    const { item } = e.currentTarget.dataset;
    wx.setStorageSync('details', item)
    wx.navigateTo({
      url: '/pages/details/details',
    })
  },

  remove(e) {
    const _this = this;
    const { item, index } = e.currentTarget.dataset;
    if (this.data.disabled) return;
    this.setData({
      disabled: true
    })

    wx.showModal({
      title: '温馨提示',
      content: '是否删除数据',
      confirmText: "删除",
      complete: async (res) => {
        _this.setData({
          disabled: false
        })
        if (res.confirm) {
          let result = await httpServer("post", "/api/Photo/delInformation", {
            id: item.id,
          })

          const list = _this.data.list;
          list.splice(index,1);
          _this.setData({
            list
          })
        }
      }
    })
  },

  //手指触摸动作开始 记录起点X坐标
  touchstart(e) {
    //开始触摸时 重置所有删除
    this.setData({
      startX: e.changedTouches[0].clientX,
      delIndex: -1
    })
  },

  //滑动事件处理
  touchmove: function (e) {
    let self = this,
      index = e.currentTarget.dataset.index, //当前索引
      startX = self.data.startX, //开始X坐标
      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
      delIndex = self.data.delIndex, // 当前滑动数据
      minClient = 50; // 最小滑动距离
    if (touchMoveX > startX) {
      //右滑
      delIndex = -1
    } else if (touchMoveX + minClient < startX) {
      //左滑
      delIndex = index
    }
    self.setData({
      delIndex
    })
  },

  goHome() {
    wx.navigateTo({
      url: '/pages/home/home',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      page: 1,
    })
    this.getListFn();
  },

  onReachBottom() {
    const { hasMore, isLoading, page } = this.data;
    if (isLoading || !hasMore) return;
    this.setData({ 
      isLoading: true,
      page: page + 1,
    });
    this.getListFn();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})